﻿/**
 * Copyright (C) 2011 original author and authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Diagnostics;

namespace NGuice.Inject.Internal.Util
{
    /// <summary>
    /// 为成员创建跟踪栈元素
    /// </summary>
    public class StackFrames
    {
        /*if[AOP]*/
        static readonly IDictionary<Type, LineNumbers> lineNumbersCache = new Dictionary<Type, LineNumbers>();
        /*end[AOP]*/

        /// <summary>
        /// TODO:LineNumbers尚未正式完成！此类暂时只能返回第一行
        /// </summary>
        public static object ForMember(MemberInfo member)
        {
            if (member == null)
            {
                return SourceProvider.UNKNOWN_SOURCE;
            }
            Type declaringClass = member.DeclaringType;
            /*if[AOP]*/
            if (!lineNumbersCache.ContainsKey(declaringClass))
            {
                lineNumbersCache.Add(declaringClass, new LineNumbers(declaringClass));
            }
            LineNumbers lineNumbers = lineNumbersCache[declaringClass];
            string fileName = lineNumbers.Source;
            int lineNumberOrNull = lineNumbers.GetLineNumber(member);
            int lineNumber = lineNumberOrNull == -1 ? lineNumbers.FirstLine : lineNumberOrNull;
            /*end[AOP]*/
            /*if[NO_AOP]
            String fileName = null;
            int lineNumber = -1;
            end[NO_AOP]*/
            Type memberType = Classes.MemberType(member);
            string memberName = memberType == typeof(ConstructorInfo) ? ".ctor" : member.Name;
            return new StackFrame(declaringClass.Name + "." + memberName, lineNumber);
        }

        /// <summary>
        /// TODO:LineNumbers尚未正式完成！此类暂时只能返回第一行
        /// </summary>
        public static object ForType(Type implementation)
        {
            if (!lineNumbersCache.ContainsKey(implementation))
            {
                lineNumbersCache.Add(implementation, new LineNumbers(implementation));
            }
            /*if[AOP]*/
            LineNumbers lineNumbers = lineNumbersCache[implementation];
            int lineNumber = lineNumbers.FirstLine;
            string fileName = lineNumbers.Source;
            /*end[AOP]*/
            /*if[NO_AOP]
            String fileName = null;
            int lineNumber = -1;
            end[NO_AOP]*/

            return new StackFrame(fileName + implementation.Name + "class", lineNumber);
        }
    }
}
